<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="python,爬虫," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="Beautiful Soup 是用Python写的一个HTML/XML的解析器，它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航（navigating），搜索以及修改剖析树的操作。它可以大大节省你的编程时间。 python2和python3的区别BeautifulSoup在python3迁移到了BS4。两个版本引入方式略有区别python2from Beau">
<meta name="keywords" content="python,爬虫">
<meta property="og:type" content="article">
<meta property="og:title" content="python BeautifulSoup框架">
<meta property="og:url" content="https://gitee.com/shuhao/2018/03/06/python BeautifulSoup框架/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="Beautiful Soup 是用Python写的一个HTML/XML的解析器，它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航（navigating），搜索以及修改剖析树的操作。它可以大大节省你的编程时间。 python2和python3的区别BeautifulSoup在python3迁移到了BS4。两个版本引入方式略有区别python2from Beau">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-03-09T02:37:56.225Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="python BeautifulSoup框架">
<meta name="twitter:description" content="Beautiful Soup 是用Python写的一个HTML/XML的解析器，它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航（navigating），搜索以及修改剖析树的操作。它可以大大节省你的编程时间。 python2和python3的区别BeautifulSoup在python3迁移到了BS4。两个版本引入方式略有区别python2from Beau">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2018/03/06/python BeautifulSoup框架/"/>





  <title> python BeautifulSoup框架 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2018/03/06/python BeautifulSoup框架/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                python BeautifulSoup框架
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-06T08:46:52+08:00">
                2018-03-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Beautiful Soup 是用Python写的一个HTML/XML的解析器，它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航（navigating），搜索以及修改剖析树的操作。它可以大大节省你的编程时间。</p>
<h2 id="python2和python3的区别"><a href="#python2和python3的区别" class="headerlink" title="python2和python3的区别"></a>python2和python3的区别</h2><p>BeautifulSoup在python3迁移到了BS4。两个版本引入方式略有区别<br>python2<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> BeautifulSoup <span class="keyword">import</span> BeautifulSoup</span><br></pre></td></tr></table></figure></p>
<p>python3<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br></pre></td></tr></table></figure></p>
<h2 id="BeautifulSoup安装"><a href="#BeautifulSoup安装" class="headerlink" title="BeautifulSoup安装"></a>BeautifulSoup安装</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ apt-get install Python-bs4 # Debain / ubuntu系统安装BeautifulSoup</span><br><span class="line"></span><br><span class="line">$ pip install beautifulsoup4 # 通过pip来安装</span><br><span class="line"></span><br><span class="line">$ easy_install beautifulsoup4 # 通过easy_install来安装</span><br></pre></td></tr></table></figure>
<h2 id="1-解析文档"><a href="#1-解析文档" class="headerlink" title="1.解析文档"></a>1.解析文档</h2><h4 id="1-1、BeautifulSoup的使用"><a href="#1-1、BeautifulSoup的使用" class="headerlink" title="1.1、BeautifulSoup的使用"></a>1.1、BeautifulSoup的使用</h4><p>BeautifulSoup将文档解析为一个BeautifulSoup。后续所有对文档的操作都是基于该对象的。<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">html_doc = &quot;&lt;html&gt;data&lt;/html&gt;&quot;</span><br><span class="line">soup = BeautifulSoup(html_doc)</span><br></pre></td></tr></table></figure></p>
<h4 id="1-2、文档解析器的类型"><a href="#1-2、文档解析器的类型" class="headerlink" title="1.2、文档解析器的类型"></a>1.2、文档解析器的类型</h4><p>在BeautifulSoup中有四种文档解析类型：</p>
<table>
<thead>
<tr>
<th style="text-align:center">解析器</th>
<th style="text-align:center">使用方法</th>
<th style="text-align:center">优势</th>
<th style="text-align:center">劣势</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">python标准库</td>
<td style="text-align:center">BeautifulSoup(doc,”html.parser”)</td>
<td style="text-align:center">1.Python的内置标准库；2.执行速度适中；3.文档容错能力强</td>
<td style="text-align:center">Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差</td>
</tr>
<tr>
<td style="text-align:center">lxm HTML解析器</td>
<td style="text-align:center">BeautifulSoup(doc,’lxml’)</td>
<td style="text-align:center">1.速度快；2.文档容错能力强</td>
<td style="text-align:center">需要安装C语言库</td>
</tr>
<tr>
<td style="text-align:center">lxm xml解析器</td>
<td style="text-align:center">BeautifulSoup(doc,’lxml,xml’)</td>
<td style="text-align:center">1.速度快；2.唯一支持xml的解析</td>
<td style="text-align:center">需要安装C语言库</td>
</tr>
<tr>
<td style="text-align:center">html5lib解析器</td>
<td style="text-align:center">BeatutifulSoup(doc, ‘html5lib’)</td>
<td style="text-align:center">1.最好容错率；2.浏览器方式解析文档；2.生产html5格式的文档</td>
<td style="text-align:center">速度慢；不依赖外部扩展</td>
</tr>
</tbody>
</table>
<h4 id="1-3、解析文档时过滤"><a href="#1-3、解析文档时过滤" class="headerlink" title="1.3、解析文档时过滤"></a>1.3、解析文档时过滤</h4><p>当我们解析一篇内容十分长的文档时，可能我们只需要解析其中某个元素例如a，或者某个属性例如：id=”btn”的内容时，我们可以使用过滤器<strong><em>SoupStrainer</em></strong>解析部分文档。从而减少内存使用，以及节约解析的时间，提高搜索的速度。<br>当我们使用BeautifulSoup解析文档时，只需要设置parse_only的值便可。<br>SoupStrainer使用：<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> SoupStrainer</span><br><span class="line"></span><br><span class="line">only_a_tag = SoupStrainer(<span class="string">"a"</span>)</span><br><span class="line">only_tags_with_id_btn = SoupStrainer(id=<span class="string">"btn"</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">is_short_string</span><span class="params">(string)</span>:</span></span><br><span class="line">	<span class="keyword">return</span> len(string) &lt; <span class="number">10</span></span><br><span class="line">only_short_strings = SoupStrainer(text=is_short_string)</span><br><span class="line"></span><br><span class="line">html_doc = <span class="string">"..."</span></span><br><span class="line">BeautifulSoup(html_doc, parse_only=only_a_tag)</span><br><span class="line">BeautifulSoup(html_doc, parse_only=only_tags_with_id_btn)</span><br><span class="line">BeautifulSoup(html_doc, parse_only=only_short_strings)</span><br></pre></td></tr></table></figure></p>
<h4 id="1-4、BeautifulSoup中对象的种类"><a href="#1-4、BeautifulSoup中对象的种类" class="headerlink" title="1.4、BeautifulSoup中对象的种类"></a>1.4、BeautifulSoup中对象的种类</h4><ol>
<li><p>Tag</p>
</li>
<li><p>NavigableString</p>
</li>
<li><p>BeautifulSoup</p>
</li>
<li><p>Comment</p>
</li>
</ol>
<h2 id="2-操作文档"><a href="#2-操作文档" class="headerlink" title="2.操作文档"></a>2.操作文档</h2><h4 id="2-1、直接查找"><a href="#2-1、直接查找" class="headerlink" title="2.1、直接查找"></a>2.1、直接查找</h4><p>当我们解析文档得到BeautifulSoup对象后，可以直接获取文档标签<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">soup = BeautifulSoup(html_doc)</span><br><span class="line">tag = soup.a</span><br><span class="line">print(tag.name) <span class="comment"># name属性</span></span><br><span class="line">tag[<span class="string">"class"</span>] <span class="comment"># a标签的class属性，多个属性返回一个属性列表</span></span><br><span class="line">pring(tag.string) <span class="comment"># 获取a标签中包含的字符串</span></span><br></pre></td></tr></table></figure></p>
<p>这样搜索只能搜索到第一个匹配的标签节点，那么如何获取其他标签节点？<br>查找其他标签节点<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">head_childrens = soup.head.contents # 获取head标签下的子节点。以列表方式返回 &lt;class &apos;list&apos;&gt;</span><br><span class="line">a_childrens = soup.a.children # 获取a标签下的子节点。返回列表迭代器对象 &lt;class &apos;list_iterator&apos;&gt;</span><br><span class="line"></span><br><span class="line">soup.a.parent # 获取直接父节点</span><br><span class="line">soup.a.parents # 递归获取所有的父节点</span><br><span class="line"></span><br><span class="line">soup.a.previous_silbing # 获取下一个兄弟节点</span><br><span class="line">soup.a.previous_siblings #获取所有的前面的兄弟节点</span><br><span class="line">soup.a.next_sibling # 获取下一个兄弟节点</span><br><span class="line">soup.a.next_siblings # 获取所有的后面的兄弟节点</span><br><span class="line"></span><br><span class="line">soup.a.previous_element # 上一个元素</span><br><span class="line">soup.a.previous_elements</span><br><span class="line">soup.a.next_element # 下一个元素</span><br><span class="line">soup.a.next_elements</span><br></pre></td></tr></table></figure></p>
<p>通过这样查找的方式有一个缺点，这样的查找只搜索第一个匹配的标签。例如soup.a只搜索到第一个a标签。那如何查询其他节点</p>
<h4 id="2-2、find方式查找"><a href="#2-2、find方式查找" class="headerlink" title="2.2、find方式查找"></a>2.2、find方式查找</h4><p>可以看出上面搜索文档树的方式不方便。BeautifulSoup为我们提供了其他的搜索方法。如find<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">soup.find_all(<span class="string">'a'</span>) <span class="comment"># 查找所有的a标签</span></span><br><span class="line">soup.find_all([<span class="string">"a"</span>, <span class="string">"li"</span>]) <span class="comment"># 查找所有的a标签和li标签</span></span><br><span class="line">soup.find_all(<span class="keyword">True</span>) <span class="comment"># 查找所有的tag不返回字符串节点</span></span><br><span class="line"></span><br><span class="line">soup.find_all(id=<span class="string">"btn"</span>) <span class="comment"># 查找所有class=btn的标签</span></span><br><span class="line">soup.find_all(id=<span class="keyword">True</span>) <span class="comment"># 查询所有包含id的标签，不论id的值是什么</span></span><br></pre></td></tr></table></figure></p>
<p>如果要查找class属性呢？因为class在python中是保留的关键字，无法像id那样直接查找。一些类似于class特殊的属性，如何查找？<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">class_icon = soup.find_all(attrs=&#123;<span class="string">"class"</span>: <span class="string">"icon"</span>&#125;) <span class="comment">#使用attrs传入一个属性的字典来进行搜索匹配</span></span><br></pre></td></tr></table></figure></p>
<p>通过css属性来find<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">class_icon = soup.find_all(&quot;i&quot;, class_=&quot;icon&quot;) # 查询所有的class=&quot;icon&quot;的i标签 class属性通过class_来标志</span><br><span class="line"></span><br><span class="line">a_text = soup.find_all(&quot;a&quot;, text=&quot;地铁&quot;) #查找所有文本为：地铁的a标签</span><br></pre></td></tr></table></figure></p>
<p>其他查找方法<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">find_parents( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_parent( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_next_siblings( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_next_sibling( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_previous_siblings( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_previous_sibling( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_all_next( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_next( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_all_previous( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line">find_previous( name , attrs , recursive , text , **kwargs )</span><br><span class="line"></span><br><span class="line"><span class="comment"># recursive  调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .</span></span><br></pre></td></tr></table></figure></p>
<h4 id="2-3、css选择器-select"><a href="#2-3、css选择器-select" class="headerlink" title="2.3、css选择器 select"></a>2.3、css选择器 select</h4><p>除了上面两种查找tag的方法，BeautifulSoup还支持CSS选择器</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#按照dom树查找</span></span><br><span class="line">soup.select(<span class="string">"title"</span>) <span class="comment"># 通过标签名查找tag</span></span><br><span class="line">soup.select(<span class="string">"body a"</span>) <span class="comment"># 通过tag标签逐层查找</span></span><br><span class="line">soup.select(<span class="string">"li &gt; a"</span>) <span class="comment"># 查找标签下的直接字标签</span></span><br><span class="line">soup.select(<span class="string">"#btn ~ .sister"</span>) <span class="comment"># 查找兄弟节点</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#按照css属性查找</span></span><br><span class="line">soup.select(<span class="string">"#btn"</span>) <span class="comment"># 查找id为btn的标签</span></span><br><span class="line">soup.select(<span class="string">".sister"</span>) <span class="comment"># 查找class="sister"的标签</span></span><br><span class="line">soup.select(<span class="string">"a[href]"</span>) <span class="comment"># 判断是否存在某个属性查找</span></span><br><span class="line">soup.select(<span class="string">"a[href="</span>http://www.baidu.com<span class="string">"]"</span>) <span class="comment"># 通过属性的值来查找</span></span><br></pre></td></tr></table></figure>
<h4 id="2-4、-修改文档"><a href="#2-4、-修改文档" class="headerlink" title="2.4、 修改文档"></a>2.4、 修改文档</h4><p>当我们读取到了文档，当然也可以对文档的内容进行修改。<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#修改标签的名称和属性</span></span><br><span class="line">tag_b = soup.b</span><br><span class="line">tag_b.name = <span class="string">"p"</span></span><br><span class="line">tag_b[<span class="string">"class"</span>] = <span class="string">"tip"</span></span><br><span class="line">tag_b[<span class="string">"id"</span>] = <span class="string">"msg"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 修改文本内容</span></span><br><span class="line">tag_b.string = <span class="string">"new msg"</span></span><br><span class="line">print(tag_b)</span><br><span class="line"><span class="comment">## &lt;p class="tip" id="msg"&gt;new msg&lt;/p&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#添加内容</span></span><br><span class="line">tag_b.append(<span class="string">"append msg"</span>)</span><br><span class="line">print(tag_b)</span><br><span class="line"><span class="comment">## &lt;p class="tip" id = "msg"&gt;new msg pappend msg&lt;/p&gt;</span></span><br></pre></td></tr></table></figure></p>
<h2 id="3-编码"><a href="#3-编码" class="headerlink" title="3.编码"></a>3.编码</h2><h4 id="3-1、输入编码"><a href="#3-1、输入编码" class="headerlink" title="3.1、输入编码"></a>3.1、输入编码</h4><p>任何HTML或XML文档都有自己的编码方式,比如ASCII 或 UTF-8,但是使用Beautiful Soup解析后,文档都被转换成了Unicode。BeautifulSoup使用编码自动检测匹配文档的编码格式，这样很慢。如果预先知道文档的编码格式，在生产BeautifulSoup对象的时候指定文档的编码格式，可以提高文档解析速度，减少文档检测编码出错的概率。通过<strong><em>from_encoding</em></strong>参数进行指定。<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">soup = BeautifulSoup(markup, from_encoding=<span class="string">"iso-8859-8"</span>)</span><br></pre></td></tr></table></figure></p>
<h4 id="3-2、输出编码"><a href="#3-2、输出编码" class="headerlink" title="3.2、输出编码"></a>3.2、输出编码</h4><p>BeautifulSoup可以将解析的文档输出。通过Beautiful Soup输出文档时,不管输入文档是什么编码方式,输出编码均为UTF-8编码。但是可以通过prettify()方法修改输出时文档的编码格式<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">print(soup.prettify(<span class="string">"latin-1"</span>))</span><br><span class="line"><span class="comment"># &lt;html&gt;</span></span><br><span class="line"><span class="comment">#  &lt;head&gt;</span></span><br><span class="line"><span class="comment">#   &lt;meta content="text/html; charset=latin-1" http-equiv="Content-type" /&gt;</span></span><br><span class="line"><span class="comment"># ...</span></span><br><span class="line">meta中编码的格式与设置的编码格式一致</span><br></pre></td></tr></table></figure></p>
<blockquote>
<blockquote>
<p>参考<br><a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/" target="_blank" rel="external">BeautifulSoup文档</a></p>
</blockquote>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/python/" rel="tag"># python</a>
          
            <a href="/blog/tags/爬虫/" rel="tag"># 爬虫</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/03/06/python urllib库/" rel="next" title="python urllib库">
                <i class="fa fa-chevron-left"></i> python urllib库
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/03/06/python-csv库/" rel="prev" title="python csv库">
                python csv库 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#python2和python3的区别"><span class="nav-number">1.</span> <span class="nav-text">python2和python3的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#BeautifulSoup安装"><span class="nav-number">2.</span> <span class="nav-text">BeautifulSoup安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-解析文档"><span class="nav-number">3.</span> <span class="nav-text">1.解析文档</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1、BeautifulSoup的使用"><span class="nav-number">3.0.1.</span> <span class="nav-text">1.1、BeautifulSoup的使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2、文档解析器的类型"><span class="nav-number">3.0.2.</span> <span class="nav-text">1.2、文档解析器的类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3、解析文档时过滤"><span class="nav-number">3.0.3.</span> <span class="nav-text">1.3、解析文档时过滤</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4、BeautifulSoup中对象的种类"><span class="nav-number">3.0.4.</span> <span class="nav-text">1.4、BeautifulSoup中对象的种类</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-操作文档"><span class="nav-number">4.</span> <span class="nav-text">2.操作文档</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1、直接查找"><span class="nav-number">4.0.1.</span> <span class="nav-text">2.1、直接查找</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2、find方式查找"><span class="nav-number">4.0.2.</span> <span class="nav-text">2.2、find方式查找</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3、css选择器-select"><span class="nav-number">4.0.3.</span> <span class="nav-text">2.3、css选择器 select</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4、-修改文档"><span class="nav-number">4.0.4.</span> <span class="nav-text">2.4、 修改文档</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-编码"><span class="nav-number">5.</span> <span class="nav-text">3.编码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1、输入编码"><span class="nav-number">5.0.1.</span> <span class="nav-text">3.1、输入编码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2、输出编码"><span class="nav-number">5.0.2.</span> <span class="nav-text">3.2、输出编码</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
